package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.SystemRole;
import com.karson.common.model.SystemRoleMenu;
import com.karson.common.request.PageParamRequest;
import com.karson.common.request.UserSearchRequest;
import com.karson.common.response.CommonResult;
import com.karson.common.response.UserResponse;
import com.karson.service.service.SystemRoleMenuService;
import com.karson.service.service.SystemRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/role")
@Tag(name = "角色管理接口")
public class RoleController {
    @Autowired
    private SystemRoleService systemRoleService;
    @Autowired
    private SystemRoleMenuService systemRoleMenuService;
    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<SystemRole>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {

        IPage<SystemRole> userResponsePage = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<SystemRole> userCommonPage =  systemRoleService.pageList(userResponsePage);
        return CommonResult.success(userCommonPage);
    }
    @Operation(summary = "更新角色信息")
    @PutMapping("/update")
    public CommonResult<IPage<SystemRole>> update(@RequestBody @ParameterObject SystemRole systemRole){
        boolean flag = systemRoleService.updateById(systemRole);
        return flag ? CommonResult.success("更新成功") : CommonResult.failed("更新失败");
    }
    @Operation(summary = "分页列表")
    @GetMapping("/getRoleById/{id}")
    public CommonResult<SystemRole> getList(@PathVariable Integer id){
        SystemRole systemRole = systemRoleService.getById(id);
        return CommonResult.success(systemRole);
    }
    @Operation(summary = "获取该角色可用权限菜单")
    @GetMapping("getPremById/{id}")
    public CommonResult<List<Integer>> getPremListById (@PathVariable Integer id){
        List<Integer> MenuIds = systemRoleService.getPremMenuList(id);
        return CommonResult.success(MenuIds);
    }
    @Operation(summary = "保存角色权限")
    @PostMapping("/savePerm/{roleId}")
    public CommonResult<String> savePerm(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds){
        LambdaQueryWrapper<SystemRoleMenu> eq = new LambdaQueryWrapper<SystemRoleMenu>().eq(SystemRoleMenu::getRid, roleId);
        systemRoleMenuService.remove(eq);
        systemRoleMenuService.saveMenuIds(roleId,menuIds);
        return CommonResult.success();
    }

}
